import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      keepAlive: true,
      title: '俄乌局势观察网',
    },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: '登录',
    },
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import('../views/Reg.vue'),
    meta: {
      title: '注册',
    },
  },
  {
    path: '/publisharticles',
    name: 'publisharticles',
    component: () => import('../views/PublishArticles.vue'),
    meta: {
      title: '发布文章',
    },
  },
  {
    path: '/article/:nid?',
    name: 'article',
    component: () => import('../views/Article.vue'),
    meta: {
      title: '文章详情',
    },
  },
  {
    path: '/searchpage/:searchItem?',
    name: 'searchpage',
    component: () => import('../views/SearchPage.vue'),
    props: true,
    meta: {
      title: '搜索结果',
    },
  },
  {
    path: '/userpage/:uid?',
    // redirect: '/userpage/mynews/:uid?',
    name: 'userpage',
    meta: {
      title: '个人主页',
      // keepAlive: true,
    },
    component: () => import('../views/UserPage.vue'),
    children: [
      {
        path: 'mynews/:uid?',
        // name:'mynews',
        component: () => import('../views/MyNews.vue'),
        meta: {
          title: '个人新闻列表',
          keepAlive: true,
        },
      },
      {
        path: 'mycomments/:uid?',
        // name:'mycomments',
        meta: {
          title: '个人评论列表',
          keepAlive: true,
        },
        component: () => import('../views/MyComments.vue'),
      },
    ],
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    // console.log("to:", to);
    // console.log("from:", from);
    // console.log("savedPosition:", savedPosition);

    if (from.path == '/article' && to.path == '/') {
      return savedPosition
    }
    return { x: 0, y: 0 }
  },
})
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  next()
})
export default router
